﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TGBTech.Core.Models.DanhMuc;
using TGB.Core.Infrastructure;

namespace TGBTech.Web.Controllers
{
    public class DanhMucController : Controller
    {
        private TGBTechDb db = new TGBTechDb();

        // GET: /DanhMuc/
        public ActionResult Index()
        {
            var list = db.DanhMuc.Where(p => p.State == true).ToList();
            return View(list);
        }

        // GET: /DanhMuc/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DanhMuc danhmuc = db.DanhMuc.Find(id);
            if (danhmuc == null)
            {
                return HttpNotFound();
            }
            return View(danhmuc);
        }

        // GET: /DanhMuc/Create
        public ActionResult Create()
        {
            return PartialView();
        }

        // POST: /DanhMuc/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Id,Name,Type")] DanhMuc danhmuc)
        {
            if (ModelState.IsValid)
            {
                danhmuc.State = true;
                db.DanhMuc.Add(danhmuc);
                db.SaveChanges();
                return Json(new {Id = danhmuc.Id, Name = danhmuc.Name, Type = danhmuc.Type });
            }

            return PartialView(danhmuc);
        }

        // GET: /DanhMuc/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DanhMuc danhmuc = db.DanhMuc.Find(id);
            if (danhmuc == null)
            {
                return HttpNotFound();
            }
            return PartialView(danhmuc);
        }

        // POST: /DanhMuc/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="Id,Name,Type")] DanhMuc danhmuc)
        {
            if (ModelState.IsValid)
            {
                danhmuc.State = true;
                db.Entry(danhmuc).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(danhmuc);
        }

        // GET: /DanhMuc/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            DanhMuc danhmuc = db.DanhMuc.Find(id);
            if (danhmuc == null)
            {
                return HttpNotFound();
            }
            return PartialView(danhmuc);
        }

        // POST: /DanhMuc/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            DanhMuc danhmuc = db.DanhMuc.Find(id);
            danhmuc.State = false;
            db.Entry(danhmuc).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
